package com.supermap.business.grid;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.grid.GridService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:zhoujian
 * @Desciption: 格点
 * @date:2019-04-25 16:50
 * @version:v1.0.0
 */
@RestController
@RequestMapping("grid")
public class GridServiceController {

    @Reference(timeout = 10000)
    private GridService gridService;

    @GetMapping("getGridValueByLonLat")
    public JSONUtils getGridValueByLonLat(Integer productId , String lonlats)
    {
        if (lonlats == null || "".equals(lonlats))
        {
            return  new JSONUtils().setMsg("目标经纬度点不能为空");
        }else if(!lonlats.contains(":")){
           return new JSONUtils().setMsg("经纬度格式异常,标准格式 lon1:lat1,lon2:lat2");
        }else{
            String[] lonlotArray = lonlats.split(",");
            List<Point2D> point2DList = new ArrayList<Point2D>();
            for (String lonlat: lonlotArray) {
                float lon = Float.parseFloat(lonlat.split(":")[0]);
                float lat = Float.parseFloat(lonlat.split(":")[1]);
                point2DList.add(new Point2D.Float(lon,lat));
            }
            return gridService.getGridValueByLonLat(productId,point2DList);
        }
    }
}